package com.wjq.sale.service.impl;

import com.alibaba.nacos.client.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjq.sale.config.ExcelBean;
import com.wjq.sale.config.ExcelUtils;
import com.wjq.sale.entity.Sale_list_goods;
import com.wjq.sale.mapper.Sale_list_goodsMapper;
import com.wjq.sale.service.ISale_list_goodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.beans.IntrospectionException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.Date;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wjq
 * @since 2020-06-04
 */
@Service
public class Sale_list_goodsServiceImpl extends ServiceImpl<Sale_list_goodsMapper, Sale_list_goods> implements ISale_list_goodsService {

    @Override
    public IPage<Sale_list_goods> selectAll(Page page, Sale_list_goods slg){
        return baseMapper.selectAll(page,slg);
    }

    @Override
    public XSSFWorkbook downLoadGoodsExcel(Sale_list_goods goodDto)throws Exception{
        //PageHelper.startPage(goodDto.getPageNo(),goodDto.getPageSize());
        Page<Sale_list_goods> page = new Page(goodDto.getPageNo(), goodDto.getPageSize());
        QueryWrapper wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(goodDto.getName())){
            wrapper.like("name",goodDto.getName());
        }
        //List<TGoods> tGoods = this.selectList(wrapper);
        //PageInfo<TGoods> pageInfo = new PageInfo<>(tGoods);
        IPage<Sale_list_goods> list = this.selectAll(page, goodDto);
        Map<Integer, List<ExcelBean>> map = new HashMap<Integer, List<ExcelBean>>();

        List<ExcelBean> excelBeans = new ArrayList<>();
        excelBeans.add(new ExcelBean("id","id",0));
        excelBeans.add(new ExcelBean("日期","sdate",0));
        excelBeans.add(new ExcelBean("客户","cname",0));
        excelBeans.add(new ExcelBean("商品编号","code",0));
        excelBeans.add(new ExcelBean("商品名称","name",0));
        excelBeans.add(new ExcelBean("商品型号","model",0));
        excelBeans.add(new ExcelBean("商品类别","gtname",0));
        excelBeans.add(new ExcelBean("单位","unit",0));
        excelBeans.add(new ExcelBean("单价","price",0));
        excelBeans.add(new ExcelBean("数量","num",0));
        map.put(0,excelBeans);
        XSSFWorkbook book = ExcelUtils.createExcelFile(Sale_list_goods.class, list.getRecords(), map, "商品列表");
        return book;
    }

    @Override
    public void uploadExcel(InputStream inputStream, String originalFilename) throws Exception {
        List<List<Object>> bankListByExcel = ExcelUtils.getBankListByExcel(inputStream, originalFilename);
        //List<Sale_list_goods> goodsList = new ArrayList<>();
        for (List<Object> objects : bankListByExcel) {
            Sale_list_goods tGoods = new Sale_list_goods();
            //tGoods.setId(Integer.valueOf(objects.get(1).toString()));
            tGoods.setSdate(Date.valueOf(objects.get(1).toString()));
            tGoods.setCname(String.valueOf(objects.get(2).toString()));
            tGoods.setCode(String.valueOf(objects.get(3).toString()));
            tGoods.setName(String.valueOf(objects.get(4).toString()));
            tGoods.setModel(objects.get(5).toString());
            tGoods.setGtname(String.valueOf(objects.get(6).toString()));
            tGoods.setUnit(objects.get(7).toString());
            tGoods.setPrice(Float.valueOf(objects.get(8).toString()));
            tGoods.setNum(Integer.valueOf(objects.get(9).toString()));
            tGoods.setTotal(Float.valueOf(1000));
            //goodsList.add(tGoods);
            baseMapper.insert(tGoods);
        }

    }
}
